package xupt.se.ttms.view.customer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;

import xupt.se.ttms.model.Movie;
import xupt.se.ttms.model.Seat;
import xupt.se.ttms.model.Studio;
import xupt.se.ttms.service.SeatSrv;
import xupt.se.ttms.service.StudioSrv;
import xupt.se.ttms.view.seat.Seat_Choose;

public class Customer_InformationUI {
	public static JFrame frame;
	public static Movie movie;
	public static JButton button=null;
	public static JButton buttons = null;
	public static Map<String,List<String>> map=null;
	public static List<String> studios=null;
	public static JPanel jpanel;
	public static String btnContent="";
	public static String timebtn="";
	
	public Customer_InformationUI(Movie movies,List<String> studio, Map<String, List<String>> map1){
		map=map1;
		movie = movies;
		studios=studio;
		logo();
	}
	
	public static void logo() {
		// TODO 自动生成的方法存根
		frame = new JFrame();
		frame.setLayout(null);
		frame.setTitle("框架");
		ImageIcon icon=new ImageIcon(movie.getImg());
		JLabel a = new JLabel(icon);
		a.setLayout(null);
		icon.setImage(icon.getImage().getScaledInstance(130,180,Image.SCALE_DEFAULT));
		a.setBounds(10, 15, 130, 180);
		init(frame,studios.get(0));
		Concert(frame);
		down(frame);
		frame.setLayout(null);
		frame.add(a);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, 850, 500);
        // 窗体大小不能改变
        frame.setResizable(false);
        // 居中显示
        frame.setLocationRelativeTo(null);
        // 窗体可见
        frame.setVisible(true);
	}
	public static void init(JFrame frames,String stu)
	{
		String s ="<html><body><p>电影Id：  "+movie.getName() + "</p><p>电影名字："+movie.getName()+"</p><p>电影评分："+movie.getScore()+"</p><p>导演："+movie.getDirector()+"</p><p>演员："+movie.getActor()+"</p><p>类型:"+movie.getType()+"</p><p>上映时间："+movie.getTime()+"</p><p>影片时长:"+movie.getDate()+"</p><p>影片简介:"+movie.getIntroduction()+"</p></body></html>";
		//String s ="<html><body><p>电影名字：理想国草坪</p><p>电影评分：7.5</p><p>导演：句话想</p><p>演员：菊花想想</p><p>类型:爆菊</p><p>上映时间：2016</p><p>影片时长:120分钟</p><p>影片简介: 理想是个大傻逼打屎是士大夫撒发生啊沙发沙发沙发暗示法萨芬发生的故事的感受到</p></body></html>";
		JLabel c = new JLabel(s);
        c.setBounds(160,0,690,220);
        c.setFont(new Font("",1,14));
        frames.add(c);
        JLabel b = new JLabel("时间  :");
        b.setBounds(30,230,40,25);
        jpanel = new JPanel();
        jpanel.setLayout(null);
        jpanel.setBounds(110, 230, 600, 26);
        frame.add(b);
        setTimeBtn(stu);
        frames.add(jpanel);
         		       	
	}
	public static void setTimeBtn(String stu)
	{
		jpanel.removeAll();
		  List<String> time= map.get(stu);
		  int i = 0;
		  for (String employee : time) {
	        	 JButton btnModPwd = new JButton(employee);
	     		btnModPwd.setFocusPainted(false) ;
	     		btnModPwd.setBounds(i, 0, 100, 25);
	     		i=i+120;
	     		btnModPwd.setMargin(new Insets(0,0,0,0));
	     		btnModPwd.setContentAreaFilled(false);
	     		btnModPwd.addActionListener(new ActionListener() {
	     			public void actionPerformed(ActionEvent Event) {
	     				if(button == null){
	     					button = btnModPwd;
	     				}
	     				else{
	     					button.setContentAreaFilled(false);
	     					button = btnModPwd;
	     					timebtn=employee;
	     				}
	     				btnModPwd.setContentAreaFilled(true);
	     				btnModPwd.setBackground(Color.gray);
	     			}
	     		});
	     		jpanel.add(btnModPwd);
	     		jpanel.repaint();
			}
	}
	public static void Concert(JFrame frame){
		Map<String, List<String>> map1=new HashMap<>();
		JLabel label = new JLabel("演出厅选择:");
		label.setBounds(15,270,70,25);
		int i=110,k=110;
		int j = 1;
		for (String str :studios){
			
			if(j<7)
			{
				JButton btnModPwd = new JButton(str);
				btnModPwd.setFocusPainted(false) ;
				btnModPwd.setBounds(i, 270, 100, 25);
				btnModPwd.setMargin(new Insets(0,0,0,0));
				btnModPwd.setContentAreaFilled(false);
				i=i+120;
				if(j==1){
					buttons = btnModPwd;
					btnContent=str;
					btnModPwd.setContentAreaFilled(true);
					btnModPwd.setBackground(Color.gray);
				}
				j++;
				btnModPwd.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent Event) {
						if(buttons == null){
							buttons = btnModPwd;
							
						}
						else{
							buttons.setContentAreaFilled(false);
							buttons = btnModPwd;
							btnContent=str;
						}
						btnModPwd.setContentAreaFilled(true);
						btnModPwd.setBackground(Color.gray);
						setTimeBtn(str);
					}
				});
				frame.add(btnModPwd);
			}
			else{
				JButton btnModPwd = new JButton(str);
				btnModPwd.setFocusPainted(false) ;
				btnModPwd.setBounds(k, 300, 100, 25);
				btnModPwd.setMargin(new Insets(0,0,0,0));
				k=k+120;
				j++;
				btnModPwd.setContentAreaFilled(false);
				btnModPwd.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent Event) {
						if(buttons == null){
							buttons = btnModPwd;
							
						}
						else{
							buttons.setContentAreaFilled(false);
							buttons = btnModPwd;
							btnContent=str;
						}
						btnModPwd.setContentAreaFilled(true);
						btnModPwd.setBackground(Color.gray);
					}
				});
				frame.add(btnModPwd);
			}
		}
      
		frame.add(label);
	}
	public static void down(JFrame frame){
		JLabel label = new JLabel("价格:                         58元/张");
		label.setBounds(30,350,200,20);
		frame.add(label);
		JButton btnModPwd3 = new JButton("返回");
		btnModPwd3.setFocusPainted(false) ;
		btnModPwd3.setBounds(240, 420, 100, 25);
		btnModPwd3.setMargin(new Insets(0,0,0,0));
		btnModPwd3.setContentAreaFilled(false);
		btnModPwd3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				Customer_MainUITmpl q = new Customer_MainUITmpl();
			}
		});
		JButton btnModPwd1 = new JButton("确定");
		btnModPwd1.setFocusPainted(false) ;
		btnModPwd1.setBounds(430, 420, 100, 25);
		btnModPwd1.setMargin(new Insets(0,0,0,0));
		btnModPwd1.setContentAreaFilled(false);
		btnModPwd1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				SeatSrv seatSrv=new SeatSrv();
				List<Seat> seats=new ArrayList<>();
				StudioSrv studioSrv=new StudioSrv();
				List<Studio> studios=new ArrayList<>();
			
				
				try {
					studios=studioSrv.Fetch(btnContent);
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				if(studios.size()<1)
					return ;
				int row=studios.get(0).getRowCount();
				int col=studios.get(0).getColCount();
				int a[][] = new int[row][col];
				try {
					seats=seatSrv.FetchStudio_id(studios.get(0).getID());
					if(seats.size()<1)
					{
						return ;
					}
					int k=0;	
					int st=0;
					for(int i=0;i<row;i++)
					{
						
						for(int j=0;j<col;j++)
						{
							String state=seats.get(k).getState();
							
							if(state.equals("已售"))
							{
								st=1;
							}
							else if (state.equals("损坏")) {
								st=2;
							}
							else {
								st=0;
							}
							k++;
							a[i][j]=st;
						}
						
					}
				
					new Seat_Choose(btnContent,a,movie,timebtn);
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
		frame.add(btnModPwd3);
		frame.add(btnModPwd1);
	}

}
